
cls
clear all
clear matrix
set more off
capture log close

cd "C:/Userdata/Shared/Dofiles/"

global dodatadf "DoData/Concurrent elections"									// DoData dofiles
global doanalysisdf "DoAnalysis/Concurrent elections"							// Doanalysis dofiles
global usingdata "E:/ProjData/Concurrent elections"								// Using data directory
global rawdata "D:/SCB_ConPol/Stata"											// Raw data directory
global output "C:/Userdata/Shared/Output/Concurrent elections"					// Output directory	

do "$dodatadf/programs.do"														//See separate program dofile
*



********************************************************************************
* Create a separate placebo data set used for bunching graphs ******************
********************************************************************************

foreach k in 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 {
	use "$rawdata/RTB/RTB_`k'", clear 
	duplicates tag LopNr, gen(dup)
	drop if dup > 0
	capture rename CivilDatum CivDat
	capture rename MedBDat MedBdat
	keep LopNr Lan Kommun Civil CivDat MedBdat Medblandnamn
	compress
	save "$usingdata/RTB_`k'.dta", replace
}

**1994-1997**
foreach k in 1994 1995 1996 1997 {													// Special case: Citienship status for 1995 are included in the file for 1998
	use "$usingdata/RTB_1998", clear 
	tostring MedBdat, replace

	gen year_ctz = substr(MedBdat, 1, 4)
	gen month_ctz = substr(MedBdat, 5, 2)
	gen day_ctz = substr(MedBdat, 7, 2)

	destring month_ctz year_ctz day_ctz, replace

	keep if trim(Medblandnamn) == "Sverige" 
	keep if year_ctz == `k' 
	
	* Assert that there are no duplicates *
	duplicates tag LopNr, gen(dup)
	assert dup == 0
	drop dup
	save "$usingdata/temp`k'", replace
}


**1998-2015**
foreach k in 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 {														// Citienship status for 1995 are included in the file for 1998
	use "$usingdata/RTB_`k'", clear 
	tostring MedBdat, replace

	gen year_ctz = substr(MedBdat, 1, 4)
	gen month_ctz = substr(MedBdat, 5, 2)
	gen day_ctz = substr(MedBdat, 7, 2)

	destring month_ctz year_ctz day_ctz, replace

	keep if trim(Medblandnamn) == "Sverige" 
	keep if year_ctz == `k' 
	

	* Assert that there are no duplicates *
	duplicates tag LopNr, gen(dup)
	assert dup == 0
	drop dup
	save "$usingdata/temp`k'", replace
}

** Adding together all years **
use "$usingdata/temp1994"
foreach k in 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 {														// Citienship status for 1995 are included in the file for 1998
	append using "$usingdata/temp`k'" 
}

** Defining variables **

* Birth data *
duplicates tag LopNr, gen(tag)
drop if tag==1																	// One person citizen-process both years. 

merge 1:1 LopNr using "$usingdata/Fodelseuppg.dta", nogen keep(1 3)
tostring FodArMan, replace
gen year_born = substr(FodArMan, 1, 4) 
gen month_born = substr(FodArMan, 5, 2)
destring year_born month_born, replace
gen age = year_ctz - year_born

* Drop indiviudals that are under 18
drop if age < 18																// Drop those that are not 18. These are not eligible to vote
drop if age == 18 & month_born > 8												// Drop if you turn 18 september - dec


duplicates drop LopNr, force

* Country of origin (grouped)*
merge 1:1 LopNr using "$usingdata/FodelselandIFAUGrupp", nogen keep(1 3)
ifaulabel	

* Drop nordic and Swedish born *
drop if (IFAUkod == 0 | IFAUkod == 26 | IFAUkod == 27 | IFAUkod == 28)			
											
*Citizenship date*
gen edate = mdy(month_ctz, day_ctz, year_ctz)

* Placebo treatment dates: Included in election roll (placebo date from the previous election)*
gen days_treat_enrolled = .

replace days_treat_enrolled = mdy(7,1,1994) - edate if year_ctz == 1994
replace days_treat_enrolled = mdy(7,1,1995) - edate if year_ctz == 1995 
replace days_treat_enrolled = mdy(7,1,1996) - edate if year_ctz == 1996 
replace days_treat_enrolled = mdy(7,1,1997) - edate if year_ctz == 1997 

replace days_treat_enrolled = mdy(8,20,1998) - edate if year_ctz == 1998 
replace days_treat_enrolled = mdy(8,20,1999) - edate if year_ctz == 1999 
replace days_treat_enrolled = mdy(8,20,2000) - edate if year_ctz == 2000 
replace days_treat_enrolled = mdy(8,20,2001) - edate if year_ctz == 2001 
					
replace days_treat_enrolled = mdy(8,15,2002) - edate if year_ctz == 2002 					
replace days_treat_enrolled = mdy(8,15,2003) - edate if year_ctz == 2003 
replace days_treat_enrolled = mdy(8,15,2004) - edate if year_ctz == 2004 
replace days_treat_enrolled = mdy(8,15,2005) - edate if year_ctz == 2005 
					
replace days_treat_enrolled = mdy(8,17,2006) - edate if year_ctz == 2006 					
replace days_treat_enrolled = mdy(8,17,2007) - edate if year_ctz == 2007 	
replace days_treat_enrolled = mdy(8,17,2008) - edate if year_ctz == 2008 
replace days_treat_enrolled = mdy(8,17,2009) - edate if year_ctz == 2009
 					
replace days_treat_enrolled = mdy(8,19,2010) - edate if year_ctz == 2010 					
replace days_treat_enrolled = mdy(8,19,2011) - edate if year_ctz == 2011 
replace days_treat_enrolled = mdy(8,19,2012) - edate if year_ctz == 2012 					
replace days_treat_enrolled = mdy(8,19,2013) - edate if year_ctz == 2013
 
**
order LopNr
compress
*
save "$usingdata/finaldata/placebo_all.dta", replace
*
use "$usingdata/finaldata/placebo_all.dta", clear
 
 

replace days_treat_enrolled=days_treat_enrolled * (-1)							// To faciliate for the reader. The figures are easier to read with the treatment window placed to the left and the control to the right. This does not change anything in the interpretation.


bysort year_ctz: egen obsyear = count(LopNr)
bysort year_ctz days_treat_enrolled: egen obsday = count(LopNr)

gen shareobsday = obsday / obsyear
collapse obsyear obsday shareobsday, by( year_ctz days_treat_enrolled )

gen electionyear = 0
replace electionyear = 1 if year_ctz == 1994 | year_ctz == 1998 | year_ctz == 2002 | year_ctz ==2006 | year_ctz == 2010

gen mandatperiod = .
drop if year_ctz > 2013
replace mandatperiod = 1 if (year_ctz == 1994 | year_ctz == 1995 | year_ctz == 1996 | year_ctz == 1997)
replace mandatperiod = 2 if (year_ctz == 1998 | year_ctz == 1999 | year_ctz == 2000 | year_ctz == 2001)
replace mandatperiod = 3 if (year_ctz == 2002 | year_ctz == 2003 | year_ctz == 2004 | year_ctz == 2005)
replace mandatperiod = 4 if (year_ctz == 2006 | year_ctz == 2007 | year_ctz == 2008 | year_ctz == 2009)
replace mandatperiod = 5 if (year_ctz == 2010 | year_ctz == 2011 | year_ctz == 2012 | year_ctz == 2013)

forvalues k = 11(1)79 {
	preserve
	reghdfe shareobsday electionyear if days_treat_enrolled > -`k' & days_treat_enrolled < `k', absorb(days_treat_enrolled mandatperiod)
	matrix define a=e(b)														
	scalar define a1=a[1,1]
	matrix define v=e(V)
	scalar define v1=sqrt(v[1,1])												
	scalar define upper=a1+1.96*v1												// Conventional standard errors												
	scalar define lower=a1-1.96*v1
	scalar define bandwidth=`k'
	matrix define rd=[nullmat(rd)\bandwidth,a1,upper,lower]
	restore	
	}
svmat rd
preserve
keep rd1 rd2 rd3 rd4 
drop if rd1 == .
sort rd1 
graph twoway (line rd2 rd1, lcolor(black)) (line rd3 rd1, lpattern(dash) lcolor(gs8)) (line rd4 rd1, lpattern(dash) lcolor(gs8)), graphregion(color(white)) xlabel(7(6)79) legend(order(1 "Election year dummy" 2 "95 % CI") region(col(white))) ytitle("Estimated coefficient") xtitle("Symmetric bandwidth in days")
graph export "$output/img/bunchingtest.pdf", replace 
restore
drop rd*


* Overlapping bunching histograms * 
*1994-1997*
twoway (bar shareobsday days_treat_enrolled if year_ctz == 1997, color(green*0.25)) (bar shareobsday days_treat_enrolled if year_ctz == 1996, color(green*0.5)) (bar shareobsday days_treat_enrolled if year_ctz == 1995, color(green*0.75))  (bar shareobsday days_treat_enrolled if year_ctz == 1994, color(green)), ytitle("Share decisions") xtitle("Running variable in days") legend(label(1 "1997") label(2 "1996") label(3 "1995") label(4 "1994")) graphregion(color(white)) xline(0, lcolor(red) lpattern(dash)) 
graph export "$output/img/overlaphist1994.pdf", replace

*1998-2001*
twoway (bar shareobsday days_treat_enrolled if year_ctz == 2001, color(green*0.25)) (bar shareobsday days_treat_enrolled if year_ctz == 2000, color(green*0.5)) (bar shareobsday days_treat_enrolled if year_ctz == 1999, color(green*0.75))  (bar shareobsday days_treat_enrolled if year_ctz == 1998, color(green)), ytitle("Share decisions") xtitle("Running variable in days") legend(label(1 "2001") label(2 "2000") label(3 "1999") label(4 "1998")) graphregion(color(white)) xline(0, lcolor(red) lpattern(dash)) 
graph export "$output/img/overlaphist1998.pdf", replace

*2002-2005*
twoway (bar shareobsday days_treat_enrolled if year_ctz == 2005, color(green*0.25)) (bar shareobsday days_treat_enrolled if year_ctz == 2004, color(green*0.5)) (bar shareobsday days_treat_enrolled if year_ctz == 2003, color(green*0.75))  (bar shareobsday days_treat_enrolled if year_ctz == 2002, color(green)), ytitle("Share decisions") xtitle("Running variable in days") legend(label(1 "2005") label(2 "2004") label(3 "2003") label(4 "2002")) graphregion(color(white)) xline(0, lcolor(red) lpattern(dash)) 
graph export "$output/img/overlaphist2002.pdf", replace

*2006-2009*
twoway (bar shareobsday days_treat_enrolled if year_ctz == 2009, color(green*0.25)) (bar shareobsday days_treat_enrolled if year_ctz == 2008, color(green*0.5)) (bar shareobsday days_treat_enrolled if year_ctz == 2007, color(green*0.75))  (bar shareobsday days_treat_enrolled if year_ctz == 2006, color(green)), ytitle("Share decisions") xtitle("Running variable in days") legend(label(1 "2009") label(2 "2008") label(3 "2007") label(4 "2006")) graphregion(color(white)) xline(0, lcolor(red) lpattern(dash)) 
graph export "$output/img/overlaphist2006.pdf", replace

*2010-2013*
twoway (bar shareobsday days_treat_enrolled if year_ctz == 2013, color(green*0.25)) (bar shareobsday days_treat_enrolled if year_ctz == 2012, color(green*0.5)) (bar shareobsday days_treat_enrolled if year_ctz == 2011, color(green*0.75)) (bar shareobsday days_treat_enrolled if year_ctz == 2010, color(green)), ytitle("Share decisions") xtitle("Running variable in days") legend(label(1 "2013") label(2 "2012") label(3 "2011") label(4 "2010")) graphregion(color(white)) xline(0, lcolor(red) lpattern(dash)) 
graph export "$output/img/overlaphist2010.pdf", replace



